import glob as gb
import cv2
import os
import subprocess
from pydub import AudioSegment

def pic_to_video(dir_path,fps,time):
    mypath = os.getcwd()
    path=dir_path
    file_list = os.listdir(path)
    file_list.remove(r'.DS_Store')
    file_list.remove(r'Thumbs.db')
    videoWriter = cv2.VideoWriter('test.mp4', cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), fps, (1600,900))

    for file in file_list:
        for i in range(fps*time):
            img  = cv2.imread(path+r'/'+file) 
            img = cv2.resize(img,(1600,900))
            videoWriter.write(img)

def merge_mp3_4(n):
    song1 = AudioSegment.from_mp3(r'/Users/tylor/Desktop/pyTool/res/gqj.mp3')
    song1=song1[0:1000*n]
    song1.export(r'/Users/tylor/Desktop/pyTool/res/1.mp3', format="mp3")
    inmp4 = r'/Users/tylor/Desktop/pyTool/test.mp4'
    inmp3 = r'/Users/tylor/Desktop/pyTool/res/1.mp3'

    cmd=f'ffmpeg -i {inmp4} -i {inmp3} -codec copy /Users/tylor/Desktop/mind.mp4'
    subprocess.call(cmd, shell=True)

path=r'/Users/tylor/Desktop/mind'
file_list = os.listdir(path)
time=3
time_n=len(file_list)*time
pic_to_video(path,30,time)
merge_mp3_4(12)